from fastapi import status
from fastapi.responses import JSONResponse, Response  # , ORJSONResponse
from typing import Union,Optional,Any,List,Dict
from pydantic import BaseModel,Field


class respJsonBase(BaseModel):
    code: int = Field(example=200)
    msg: str = Field(example="查询成功")
    data: list[str, Any] = []


class respJsonBaseLogin(BaseModel):
    code: int = Field(example=200)
    msg: str = Field(example="登录成功")
    access_token: str
    token_type: str = Field(example="bearer")

class StandardResponse:
    # 定义一个依赖函数，用于生成标准响应
    def generate_standard_response(status: int, message: str):
        response_data = {
            'code': status, 
            'msg': message,
            'data': []
        }
        return respJsonBase(**response_data)
    
    def login_success(status: int, message: str, token_type: str):
        response_data = {
            'code': status,
            'msg': message,
            'access_token': '',
            'token_type': token_type,
        }
        return respJsonBaseLogin(**response_data)

    
class RespSuccessJson:
    # 创建一个依赖注入项
    def resp_200():
        return StandardResponse.generate_standard_response(status=status.HTTP_200_OK, message="查询成功")
    
    def resp_201():
        return StandardResponse.generate_standard_response(status=status.HTTP_201_CREATED, message="新增成功")

    def resp_202():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="修改成功")
    
    def resp_202_reset_password():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="密码重置成功")
    
    def resp_202_modify_password():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="密码修改成功")
    
    def resp_202_publish():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="发布成功")
    
    def resp_202_configure():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="配置成功")
    
    def resp_202_push():
        return StandardResponse.generate_standard_response(status=status.HTTP_202_ACCEPTED, message="推送成功")
    
    def resp_204():
        return StandardResponse.generate_standard_response(status=status.HTTP_204_NO_CONTENT, message="删除成功")

    def resp_200_login():
        return StandardResponse.login_success(status=status.HTTP_200_OK, message="登录成功", token_type="bearer")
    

def resp_400(*, data: str = None, message: str="BAD REQUEST") -> Response:
    return JSONResponse(
        status_code=status.HTTP_400_BAD_REQUEST,
        content={
            'code': 400,
            'msg': message,
            'data': data,
        }
    )

def resp_401(*, data: str = None, message: str="无访问权限") -> Response:
    return JSONResponse(
        status_code=status.HTTP_401_UNAUTHORIZED,
        content={
            'code': 400,
            'msg': message,
            'data': data,
        }
    )

